#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>

#include "callbacks.h"
#include "interface.h"
#include "support.h"
#include "crypt.h"
#include "gui_al.h"
#include "gui_io.h"
#include "error.h"
#include "about.h"
#include "fileChooser.h"
#include "months.h"
#include "prefDialogue.h"
#include "fileChooser.h"
#include "structure.h"
#include "tabs.h" 
#include <gdk/gdkkeysyms.h>
#include "journal.h"
#include "calendar.h"
#include "second.h"





int __srchWait(void *p)
{
	char *txt = (char *) p;
	usleep(205000);
	const char *txt2 =
	    (const char *) gtk_entry_get_text(GTK_ENTRY(_menu_srch_entry));
	if (strcmp(txt, txt2) == 0)
		elog_tab_act_search();
	free(txt);
	return 0;
}

gboolean on_srch_bar_key_release(GtkWidget * widget,
				 GdkEventKey * event, gpointer user_data)
{
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCK_CALLBACK);

	char *txt;
	elog_sp_cat(&txt,
		    (char *)
		    gtk_entry_get_text(GTK_ENTRY(_menu_srch_entry)), NULL);
	elog_scnd_run_arg(__srchWait, "key wait", txt);

	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCKED);
	return FALSE;
}

gboolean on_srch_bar_btn_release(GtkEntry * entry,
				 gchar * arg1, gpointer user_data)
{
	//gtk is screwy and stupid about this.
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCK_CALLBACK);
	elog_tab_act_search();
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCKED);
	return FALSE;
}





void on_window_main_resize(GtkContainer * container, gpointer user_data)
{
	int width, height;
	gtk_window_get_size(GTK_WINDOW(container), &width, &height);
	elog_set_mainHeight(&height);
	elog_set_mainWidth(&width);
}

void
on_text_notebook_switch_page(GtkNotebook * notebook,
			     GtkNotebookPage * page,
			     guint page_num, gpointer user_data)
{
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCK_CALLBACK);
	elog_tab_set((int) page_num);
	/*  if (page_num == 0)
	   {
	   elog_gui_io_save();
	   elog_gui_io_dispDay(NULL);
	   }
	   else if (page_num != 1)
	   elog_gui_io_save(); */
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCKED);
}


void
on_treeview1_row_activated(GtkTreeView * treeview,
			   GtkTreePath * path,
			   GtkTreeViewColumn * column, gpointer user_data)
{
	//this is the callback for the search result list!      

	GtkTreeModel *model = gtk_tree_view_get_model(treeview);
	GtkTreeIter iter;
	gtk_tree_model_get_iter(model, &iter, path);
	char *title;
	char *pathstr;
	gtk_tree_model_get(model, &iter, 0, &title, 4, &pathstr, -1);
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCK_CALLBACK);
	elog_gui_io_open(pathstr, NULL);
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCKED);
}



void on_preferences_activate(GtkMenuItem * menuitem, gpointer user_data)
{
	create_pref_window();

	//gtk_widget_show_all(win);

}



gboolean on_window_main_close(GtkWidget * widget,
			      GdkEvent * event, gpointer user_data)
{

	gtk_main_quit();

	return TRUE;
}









void
on_res_srch_row_activate(GtkTreeView *treeview, gpointer user_data)
{

	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCK_CALLBACK);
	GtkTreeModel *model = (GtkTreeModel *)
	    gtk_tree_view_get_model(GTK_TREE_VIEW(_res_srch_list));
	GtkTreePath *tree_path;
	GtkTreeViewColumn *trash;
	gtk_tree_view_get_cursor(GTK_TREE_VIEW(_res_srch_list), &tree_path,
				 &trash);

	if (!tree_path)
		return ;

	char *str_path = (char *) gtk_tree_path_to_string(tree_path);

	gtk_tree_path_free(tree_path);
	GtkTreeIter iter;
	gtk_tree_model_get_iter_from_string(model, &iter, str_path);
	free(str_path);
		
	char *title;
	int n;
	gtk_tree_model_get(model, &iter, 0, &title, 1, &n, -1);

	elog_tab_act_res(title, n);
	//gtk_widget_grab_focus(_res_srch_list);

	free(title);
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCKED);
	return ;
}

gboolean on_res_pane_set(GtkWidget * widget,
			 GdkEventButton * event, gpointer user_data)
{
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCK_CALLBACK);
	char *name;
	char *n = elog_sp_shortToString(elog_tab_current());
	elog_sp_cat(&name, "SEARCH_DIVIDE_", n, NULL);
	elog_set_set_int(name,
			 gtk_paned_get_position(GTK_PANED
						(_res_srch_pane)));
	free(name);
	free(n);
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCKED);
	return FALSE;
}

